/**
 * 
 */
package com.project.tructc.MBean;

import java.io.IOException;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.http.HttpServletRequest;

import com.project.entity.Account;
import com.project.entity.PropForRent;
import com.project.entity.PropForSale;
import com.project.entity.Property;
import com.project.entity.Property_Image;
import com.project.entity.RoomForRent;
import com.project.entity.VisitLog;
import com.project.ryudo.SessionBean.AccountBeanRemote;
import com.project.ryudo.SessionBean.VisitLogBeanRemote;
import com.project.ryudo.SessionBean.requestPropertyBeanRemote;
import com.project.util.MessageCall;

/**
 * @author RYUDO
 *
 */
@ManagedBean(name="mRequestProperty")
@RequestScoped
public class MRequestProperty {
	
	private Property prop;
	private PropForRent pfr;
	private PropForSale pfs;
	private RoomForRent rfr;
	private String PropType = null;
	private Account acc;
	private String RealPropType = null;
	private String[] requested_images = new String[6];
	
	private boolean hasProperty = false;
	
	private String PIDParam;
	public MRequestProperty() 
	{
		
	}

	

	public void setPIDParam(String pIDParam) {
		PIDParam = pIDParam;
	}



	public String getPIDParam() {
		return PIDParam;
	}



	public void setHasProperty(boolean hasProperty) {
		this.hasProperty = hasProperty;
	}



	public boolean isHasProperty() throws NamingException {
		this.view_request_property();
		return hasProperty;
	}



	public String[] getRequested_images() {
		return requested_images;
	}

	public void setRequested_images(String[] requested_images) {
		this.requested_images = requested_images;
	}

	public void setRealPropType(String realPropType) {
		RealPropType = realPropType;
	}



	public String getRealPropType() {
		return RealPropType;
	}



	public Property getProp() {
		return prop;
	}



	public void setProp(Property prop) {
		this.prop = prop;
	}



	public PropForRent getPfr() {
		return pfr;
	}



	public void setPfr(PropForRent pfr) {
		this.pfr = pfr;
	}



	public PropForSale getPfs() {
		return pfs;
	}



	public void setPfs(PropForSale pfs) {
		this.pfs = pfs;
	}



	public RoomForRent getRfr() {
		return rfr;
	}



	public void setRfr(RoomForRent rfr) {
		this.rfr = rfr;
	}



	public String getPropType() {
		return PropType;
	}



	public void setPropType(String propType) {
		PropType = propType;
	}



	public Account getAcc() {
		return acc;
	}



	public void setAcc(Account acc) {
		this.acc = acc;
	}

	public void visit_log() throws NamingException, SecurityException, IOException
	{
	
		try
		{
			HttpServletRequest request = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
			Context ctx = new InitialContext();
			requestPropertyBeanRemote propRe = (requestPropertyBeanRemote) ctx
					.lookup("MySweetHome/requestPropertyBean/remote");
			List result = propRe.requestedProperty(this.PIDParam);
			
			MAccount mAcc = (MAccount)request.getSession().getAttribute("mAccount");	
		
			if(result.size() != 0 && mAcc.getAcc().getEmail() != null)
			{
				 MAccount sesAcc = (MAccount)request.getSession().getAttribute("mAccount");
				    
					Property prop2 = (Property)result.get(0);
							
				
					Context ctx2 = new InitialContext();
					VisitLogBeanRemote visitlogre = (VisitLogBeanRemote) ctx2
									.lookup("MySweetHome/VisitLogBean/remote");
					visitlogre.addLog(sesAcc.getAcc(),prop2);
			}
		} catch (Exception e)
		{
			
		}
		
		
	}

	public void view_request_property() throws NamingException
	{
		HttpServletRequest request = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
		if(request.getParameter("pid") == null)
		{
			MessageCall.errorMessage("Property not found !", "user_not_found");
			this.hasProperty = false;
		}
		else
		{
			Context ctx = new InitialContext();
			requestPropertyBeanRemote propRe = (requestPropertyBeanRemote) ctx
					.lookup("MySweetHome/requestPropertyBean/remote");
			List result = propRe.requestedProperty(request.getParameter("pid"));
			if(result.size() != 0)
			{
				
				this.prop = (Property)result.get(0);	
				List resultAccount = propRe.requestedAccount(this.prop.getAccount().getAccID());
				this.acc = (Account) resultAccount.get(0);
				
				
				
				String[] splitedstring = request.getParameter("pid").split("-");
				if(splitedstring[0].equals("P"))
				{
					this.PropType = "pfr";
					this.RealPropType = "Property For Rent";
					List resultType = propRe.requestedPropertyType(request.getParameter("pid"), this.PropType);
					this.pfr = (PropForRent)resultType.get(0);
				} else if(splitedstring[0].equals("S"))
				{
					this.PropType = "pfs";
					this.RealPropType = "Property For Sale";
					List resultType = propRe.requestedPropertyType(request.getParameter("pid"), this.PropType);
					this.pfs = (PropForSale)resultType.get(0);
				} else if(splitedstring[0].equals("R"))
				{
					this.PropType = "rfr";
					this.RealPropType = "Room For Rent";
					List resultType = propRe.requestedPropertyType(request.getParameter("pid"), this.PropType);
					this.rfr = (RoomForRent)resultType.get(0);
				}
				
				
				List resultImages = propRe.requestedImages(request.getParameter("pid"));
				int i = 0;
				if(resultImages.size() != 0)
				{
					Iterator ite = resultImages.iterator();
					while(ite.hasNext())
					{
						Property_Image propImg = (Property_Image)ite.next();
						this.requested_images[i] = propImg.getPath();
						i++;
					}
					for(int j = i;j<6;j++)
					{
						this.requested_images[j] = "";
					}
				}
				
				this.hasProperty = true;
			}
			else
			{
				MessageCall.errorMessage("Property not found !", "user_not_found");
				this.hasProperty = false;
			}
		}
	}
	
	
}
